.importzp sp
.export __line, __box, __tee, __left_l, __right_l, __left_s, __right_s
.export _setSprite


ZP_LO = $fe
ZP_HI = $ff

CD_LO = $fc
CD_HI = $fd

SP_LO = $fa
SP_HI = $fb

SPRITE1 = $0840
SPRITE2 = $0880

CHARSET = $3000

.data
CHARDATA: .word CHARSET
CURRENT_CHAR: .byte $00
CURRENT_INDEX: .byte $00
SPRITE_COUNT: .byte $00

.code

.proc _setSprite
		sei
		; fastcall
		tay

		; store ZP to stack.
		lda ZP_LO
		pha
		lda ZP_HI
		pha
		lda CD_LO
		pha
		lda CD_HI
		pha
		lda SP_LO
		pha
		lda SP_HI
		pha
		
		; fastcall contd
		sty ZP_LO
		stx ZP_HI

		lda #<SPRITE2
		sta SP_LO
		lda #>SPRITE2
		sta SP_HI

		ldy #00
		lda #$00
:		sta (SP_LO), y
		iny
		cpy #$64
		bne :-

		lda #<SPRITE1
		sta SP_LO
		lda #>SPRITE1
		sta SP_HI

		ldy #00
		lda #$00
:		sta (SP_LO), y
		iny
		cpy #$64
		bne :-

		; Initialize 
		lda #$00
		sta SPRITE_COUNT
		sta CURRENT_CHAR
		sta CURRENT_INDEX
		lda #<CHARSET
		sta CHARDATA
		lda #>CHARSET
		sta CHARDATA + 1


fetch_next:
		lda #<CHARSET
		sta CHARDATA
		lda #>CHARSET
		sta CHARDATA + 1

		ldy CURRENT_INDEX
		lda (ZP_LO), y
		tax

		; Move to the current character
:		clc
		lda CHARDATA
		adc #$08
		sta CHARDATA
		sta CD_LO
		lda CHARDATA + 1
		adc #$00
		sta CHARDATA + 1
		sta CD_HI
		dex
		bne :-

		ldx #$00
:		txa
		tay
		lda (CD_LO),y
		ldy lookup,x
		sta (SP_LO),y
		inx
		cpx #8
		bne :-

increment_current_index:
		lda CURRENT_INDEX
		clc
		adc #1
		sta CURRENT_INDEX
		cmp #3
		bne :+
		lda #4
		sta CURRENT_INDEX
		jsr incrementSP
		jmp fetch_next
:		cmp #7
		bne :+
		lda #8
		sta CURRENT_INDEX
		jsr incrementSP
		jmp fetch_next
:		cmp #11
		bne :+
		lda #12
		sta CURRENT_INDEX
		jsr incrementSP
		jmp fetch_next
:		cmp #15
		beq done

		jsr incrementSP
		jmp fetch_next

done:
		; restore ZP from stack.
		pla
		sta SP_HI
		pla
		sta SP_LO
		pla
		sta CD_HI
		pla
		sta CD_LO
		pla
		sta ZP_HI
		pla
		sta ZP_LO
		rts
.endproc

incrementSP:
		clc
		lda SP_LO
		adc #1
		sta SP_LO
		lda CURRENT_INDEX
		cmp #4
		bne :+
		lda #<SPRITE1
		adc #23
		sta SP_LO
		lda CURRENT_INDEX
:		cmp #8
		bne :+
		lda #<SPRITE2
		sta SP_LO
		lda CURRENT_INDEX
:		cmp #12
		bne :+
		lda #<SPRITE2
		adc #23
		sta SP_LO
		cli
:		rts

.rodata
__line:
	; UP
.byte	80, 32, 32, 32
.byte	84, 32, 32, 32
.byte	84, 32, 32, 32
.byte	88, 32, 32, 32

	; LEFT
.byte	89, 90, 90, 91
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	80, 32, 32, 32
.byte	84, 32, 32, 32
.byte	84, 32, 32, 32
.byte	88, 32, 32, 32

	; RIGHT
.byte	89, 90, 90, 91
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32


__box:
	; UP
.byte	87, 87, 32, 32
.byte	87, 87, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	87, 87, 32, 32
.byte	87, 87, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	87, 87, 32, 32
.byte	87, 87, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	87, 87, 32, 32
.byte	87, 87, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32


__tee:
	; UP
.byte	32, 86, 32, 32
.byte	86, 86, 86, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	32, 86, 32, 32
.byte	86, 86, 32, 32
.byte	32, 86, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	86, 86, 86, 32
.byte	32, 86, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	86, 32, 32, 32
.byte	86, 86, 32, 32
.byte	86, 32, 32, 32
.byte	32, 32, 32, 32

__left_l:
	; UP
.byte	83, 83, 32, 32
.byte	32, 83, 32, 32
.byte	32, 83, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	83, 83, 83, 32
.byte	83, 32, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	83, 32, 32, 32
.byte	83, 32, 32, 32
.byte	83, 83, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	32, 32, 83, 32
.byte	83, 83, 83, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32


__right_l:
	; UP
.byte	85, 85, 32, 32
.byte	85, 32, 32, 32
.byte	85, 32, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	85, 85, 85, 32
.byte	32, 32, 85, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	32, 85, 32, 32
.byte	32, 85, 32, 32
.byte	85, 85, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	85, 32, 32, 32
.byte	85, 85, 85, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32


__left_s:
	; UP
.byte	81, 32, 32, 32
.byte	81, 81, 32, 32
.byte	32, 81, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	32, 81, 81, 32
.byte	81, 81, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	81, 32, 32, 32
.byte	81, 81, 32, 32
.byte	32, 81, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	32, 81, 81, 32
.byte	81, 81, 32, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32


__right_s:
	; UP
.byte	32, 82, 32, 32
.byte	82, 82, 32, 32
.byte	82, 32, 32, 32
.byte	32, 32, 32, 32

	; LEFT
.byte	82, 82, 32, 32
.byte	32, 82, 82, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

	; DOWN
.byte	32, 82, 32, 32
.byte	82, 82, 32, 32
.byte	82, 32, 32, 32
.byte	32, 32, 32, 32

	; RIGHT
.byte	82, 82, 32, 32
.byte	32, 82, 82, 32
.byte	32, 32, 32, 32
.byte	32, 32, 32, 32

lookup: 
	.byte 0, 3, 6, 9, 12, 15, 18, 21
